package com.navidove.appspotlight.model.bo;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

@Entity
@Table(name = "app_item", uniqueConstraints = @UniqueConstraint(columnNames = "app_name"))
public class AppItem implements java.io.Serializable {

	private static final long serialVersionUID = 1L;

	private Long id;
	private AppDictionary appCategory;
	private AppDictionary appSource;
	private String appName;
	private String appIntro;
	private String appFeature;
	private String owner;
	private Boolean enable;
	private Date crtAt;
	private Date updAt;
	private Map<String, AppDevice> appDeviceMap = new HashMap<String, AppDevice>();

	@Id
	@SequenceGenerator(name = "generator", sequenceName = "app_item_id_seq", allocationSize = 1)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "app_category", nullable = false)
	public AppDictionary getAppCategory() {
		return this.appCategory;
	}

	public void setAppCategory(AppDictionary appCategory) {
		this.appCategory = appCategory;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "app_source", nullable = false)
	public AppDictionary getAppSource() {
		return this.appSource;
	}

	public void setAppSource(AppDictionary appSource) {
		this.appSource = appSource;
	}

	@Column(name = "app_name", unique = true, nullable = false, length = 100)
	public String getAppName() {
		return this.appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	@Column(name = "app_intro", nullable = true)
	public String getAppIntro() {
		return this.appIntro;
	}

	public void setAppIntro(String appIntro) {
		this.appIntro = appIntro;
	}

	@Column(name = "app_feature", nullable = true)
	public String getAppFeature() {
		return this.appFeature;
	}

	public void setAppFeature(String appFeature) {
		this.appFeature = appFeature;
	}

	@Column(name = "owner", nullable = false, length = 100)
	public String getOwner() {
		return this.owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	@Column(name = "enable", nullable = false)
	public Boolean getEnable() {
		return this.enable;
	}

	public void setEnable(Boolean enable) {
		this.enable = enable;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "crt_at", length = 29)
	public Date getCrtAt() {
		return this.crtAt;
	}

	public void setCrtAt(Date crtAt) {
		this.crtAt = crtAt;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "upd_at", length = 29)
	public Date getUpdAt() {
		return this.updAt;
	}

	public void setUpdAt(Date updAt) {
		this.updAt = updAt;
	}
	
	@OneToMany(mappedBy="appItem")
	@MapKey(name="deviceType")
	public Map<String, AppDevice> getAppDeviceMap() {
		return appDeviceMap;
	}

	public void setAppDeviceMap(Map<String, AppDevice> appDeviceMap) {
		this.appDeviceMap = appDeviceMap;
	}

	/**
	 * @see java.lang.Object#toString
	 * @return String
	 */
	public String toString() {
		return new ToStringBuilder(this).append("id", getId()).toString();

	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}
		AppItem rhs = (AppItem) obj;

		return new EqualsBuilder().append(getId(), rhs.getId()).isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(getId()).toHashCode();
	}

}
